package com.jrx.anytxn.param.controller.product;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.product.PrDelqTableReq;
import com.jrx.anytxn.param.dto.product.PrDelqTableRes;
import com.jrx.anytxn.param.dto.product.PrGuaranteeTableReq;
import com.jrx.anytxn.param.dto.product.PrGuaranteeTableRes;
import com.jrx.anytxn.param.dto.sms.PrSmsTemplateTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrGuaranteeTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * 代偿回购参数 Controller
 */
@Api(tags = "页面代偿回购参数API接口")
@RestController
@RequestMapping(value = "/param/guarantee")
public class PrGuaranteeController {

    private static final Logger logger = LoggerFactory.getLogger(PrGuaranteeController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPrGuaranteeTableService prGuaranteeTableService;


    @ApiOperation(value = "根据租户ID和代偿回购参数ID查询代偿回购参数表信息", notes = "根据租户ID和代偿回购参数ID查询代偿回购参数表信息")
    @GetMapping(value = "/{tenantId}/{guaranteeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "guaranteeId", value = "代偿回购参数表ID", required = true)
    })
    public TxnRespResult<PrGuaranteeTableRes> getByTenIdAndGuaranteeId(@NotNull @PathVariable String tenantId, @PathVariable String guaranteeId) {
        TxnRespResult<PrGuaranteeTableRes> result = new TxnRespResult<>();
        PrGuaranteeTableRes res;
        try {
            res = prGuaranteeTableService.findByTenIdAndGuaranteeId(String.valueOf(infoService.getUser().getProjectId()), guaranteeId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询代偿回购参数表信息", notes = "分页查询代偿回购参数表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrGuaranteeTableRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrGuaranteeTableRes>> result = new TxnRespResult<>();
        TxnPage<PrGuaranteeTableRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = prGuaranteeTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增代偿回购参数表信息", notes = "新增代偿回购参数表信息")
    @PostMapping("/")
    public TxnRespResult<PrGuaranteeTableRes> createGuaranteeTable(@RequestBody PrGuaranteeTableReq prGuaranteeTableReq) {
        PrGuaranteeTableRes res;
        TxnRespResult<PrGuaranteeTableRes> result = new TxnRespResult<>();
        try {
            prGuaranteeTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prGuaranteeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = prGuaranteeTableService.addPrGuaranteeTable(prGuaranteeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID编辑代偿回购参数表信息", notes = "根据租户ID编辑代偿回购参数表信息")
    @PutMapping("/")
    public TxnRespResult<PrGuaranteeTableRes> modifyDelqTable(@RequestBody PrGuaranteeTableReq prGuaranteeTableReq) {
        TxnRespResult<PrGuaranteeTableRes> result = new TxnRespResult<>();
        PrGuaranteeTableRes res;
        try {
            prGuaranteeTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = prGuaranteeTableService.modifyGuaranteeTable(prGuaranteeTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "根据租户ID和代偿回购参数ID删除表信息", notes = "根据租户ID和代偿回购参数ID删除表信息")
    @DeleteMapping(value = "/{tenantId}/{guaranteeId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "guaranteeId", value = "代偿回购参数ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndGuaranteeId(@NotNull @PathVariable String tenantId, @PathVariable String guaranteeId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = prGuaranteeTableService.removeByTenIdAndGuaranteeId(String.valueOf(infoService.getUser().getProjectId()), guaranteeId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "获得代偿回购参数下拉信息", notes = "获得代偿回购参数下拉信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<PrGuaranteeTableRes>>> getAllSmsTemplateList() {
        TxnRespResult<HashMap<String, List<PrGuaranteeTableRes>>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PrGuaranteeTableRes> allGuaranteeTable = prGuaranteeTableService.findAllGuaranteeByTenId(tenantId);
        HashMap<String, List<PrGuaranteeTableRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allGuaranteeTable);
        return result.getSuccess(sysCodeMap);
    }

}
